package com.dingding.service.store.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.dingding.common.core.base.dao.impl.BaseDaoImpl;
import com.dingding.common.core.beans.CouponStore;
import com.dingding.common.core.util.ToolUtil;
import com.dingding.facde.activity.dto.CouponStoreDTO;
import com.dingding.facde.activity.dto.sql.CouponCitySQL;
import com.dingding.service.store.dao.ICouponStoreDao;

/**
 * 
 * @ClassName: CityImpl 
 * @Description: 城市列表操作DAO实现
 * @author CHEHAUNBO 
 * @date 2016年2月3日 下午5:27:34
 * 
 */
@Component("couponStoreDao")
public class CouponStoreDaoImpl extends BaseDaoImpl<CouponStore> implements ICouponStoreDao {
	
	private Logger log = Logger.getLogger(this.getClass());
	

	/**
	 * @Title: getStroeList
	 * @author fengshuonan
	 * @return List<CouponStore> 门店列表集合
	 * @Override
	 */
	public List<CouponStore> getStroeList() {
		
		List<CouponStore> list = new ArrayList<CouponStore>();
		
		try {
			list = queryAll();
		} catch (Exception e) {
			log.error("查询所有门店信息出错！CouponStoreDaoImpl：getStroeList()",e);
		}
		
		return list;
	}


	/**
	 * @Title: getStoreInfo
	 * @Description: 获取门店详情
	 * @param storeId 门店id
	 * @author fengshuonan
	 * @return CouponStore 门店详情
	 * @throws
	 */
	public CouponStore getStoreInfo(String storeId){
		
		CouponStore couponStore = new CouponStore();
		
		try {
			couponStore = getById(storeId);
		} catch (Exception e) {
			log.error("获取门店详情出错！getStoreInfo()",e);
		}
		
		return couponStore;
	}

	/**
	 * @Title: getCouponStoreList 
	 * @Description: 根据城市名称获取门店列表
	 * @param cityName 城市名称
	 * @author YHQ
	 * @return List<CouponStoreDTO>
	 * @throws
	 */
	@Override
	@Transactional(readOnly=true)
	public List<CouponStoreDTO> getCouponStoreList(String cityName) {
		
		List<Object[]> objects = new ArrayList<Object[]>();

		try {
			objects = super.listSqlObj(CouponCitySQL.GET_USER_COUPON_SQL, cityName);
		} catch (Exception e) {
			log.error("根据用户ID获取所有优惠券出现异常！", e);
		}

		return getCouponStoreListConverBean(objects);
		
	}

	/**
	 * @Title: getCouponStoreListConverBean 
	 * @Description: 类型转换
	 * @param objects 城市门店信息
	 * @author YHQ
	 * @return List<CouponStoreDTO> 返回值
	 * @throws
	 */
	public List<CouponStoreDTO> getCouponStoreListConverBean(List<Object[]> objects){
		
		List<CouponStoreDTO> couponStoreDTOs = new ArrayList<CouponStoreDTO>();
		
		for (Object[] obj : objects) {
			
			CouponStoreDTO couponStoreDTO = new CouponStoreDTO();
			
			//门店名称
			if(obj[0] != null){
				couponStoreDTO.setStoreName(obj[0].toString());
			}
			//门店联系电话
			if(obj[1] != null){
				couponStoreDTO.setStoreTel(obj[1].toString());
			}
			//门店地址
			if(obj[2] != null){
				couponStoreDTO.setStoreAddress(obj[2].toString());
			}
			//门店经度
			if(obj[3] != null){
				couponStoreDTO.setLongitude(ToolUtil.castDouble(obj[3]));
			}
			//门店纬度
			if(obj[4] != null){
				couponStoreDTO.setLatitude(ToolUtil.castDouble(obj[4]));
			}
			//营业时间
			if(obj[5] != null){
				couponStoreDTO.setBusinessTime(obj[5].toString());
			}
			
			couponStoreDTOs.add(couponStoreDTO);
		}
		
		return couponStoreDTOs;
	}
}
